package jtk.project4.fleet.screen.AddTireInventory;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelTireAction;
import jtk.project4.fleet.action.SubmitTireAction;
import jtk.project4.fleet.domain.TireInv;
import jtk.project4.fleet.event.TireInventoryEvent;
import jtk.project4.fleet.field.GeneralInfoAddEditTireToInventoryField;
import jtk.project4.fleet.field.PurchaseInfoAddEditTireToInventoryField;
import jtk.project4.fleet.field.StatisticsAddEditTireToInventoryField;
import jtk.project4.fleet.field.UnderWarrantyAddEditTireToInventoryField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;
import nl.coderight.jazz.form.validation.ValidationEvent;

public class AddTireInventoryView extends FormView{
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	private Action submitAction;
	private Action cancelAction;
	
	
	public AddTireInventoryView (){
		setSize(500,400);
		center();
	}
	public void handleEvent(TireInventoryEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SUBMIT:
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close();
				// fall-through!
			}
			System.out.print("save");
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}

	
	
	
	@Override
	protected void init() {
		createAction();
		createFields();
		createLayout();		
	}	
	
	
	
	private void createAction() {
		submitAction = new SubmitTireAction();
		cancelAction = new CancelTireAction();
		// enable submit button when form is changed
		//setOnChangeAction(submitAction);
	}
	
	private void createFields() {
		submitButton = new PushButton(submitAction);
		cancelButton = new PushButton(cancelAction);
	}
	
	private void createLayout() {
		setHeader(createHeader());
		addField(createForm());
	}
	private FormHeader createHeader() {
		// TODO Auto-generated method stub
		formHeader = new FormHeader();
		formHeader.setTitle("tire.header.form");
		return formHeader;
	}
//	private FormField createForm()
//	{
//		GroupControl form = new GroupControl();
//		form.addField(new LeftAddEditTireToInventoryField("general"));
//		form.addField(new RightAddEditTireToInventoryField("nformation"));
//		//form.addField(new GeneralInfoAddEditTireToInventoryField("General"));
//		form.addField(createButtons(), SOUTH);
//		
//		return form;
//	}
//	
//	private GroupControl createButtons() {
//		GroupControl buttons = new GroupControl();
//		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
//		buttons.addField(submitButton);
//		buttons.addField(cancelButton);
//		return buttons;
//	}
	private GroupControl createGeneralTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(createLeftLayout())
		.addField(createRightLayout());		
		return form;
	}
	
	private GroupControl createRightLayout(){
		GroupControl form = new GroupControl();
		
		form.setLayout (new FormLayout())
		.addField(new PurchaseInfoAddEditTireToInventoryField ("TireInv"))
		.addRow()
		.addField(new StatisticsAddEditTireToInventoryField ("TireInv"))
		.addRow()
		.addField(new UnderWarrantyAddEditTireToInventoryField ("TireInv"));
		
		return form;
	}
	
	private GroupControl createLeftLayout(){
		GroupControl form = new GroupControl();
		
		form.setLayout (new FormLayout())
		.addField(new GeneralInfoAddEditTireToInventoryField ("TireInv"));		
		return form;
	}
	
	
	private GroupControl createForm()
	{
		GroupControl form = new GroupControl();
		
		form.addField(createGeneralTab());
		form.addField(createButtons(), SOUTH);
		
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
}
